package com.xbongbong.pro.statistic.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.MD5Util;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.dao.BiDataCorrDao;
import com.xbongbong.pro.domain.dao.BiDataCorrelationDao;
import com.xbongbong.pro.domain.entity.BiDataCorrEntity;
import com.xbongbong.pro.statistic.model.BiDataCorrModel;
import com.xbongbong.pro.statistic.model.BiDataCorrelationModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/3/20 10:16
 * @since v1.0
 */
@Service("biDataCorrModel")
public class BiDataCorrModelImpl implements BiDataCorrModel {
    @Resource
    private BiDataCorrDao dao;
    @Override
    public Integer insert(BiDataCorrEntity entity){
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return dao.insert(entity);
    }
    @Override
    public Integer update(BiDataCorrEntity entity){
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return dao.update(entity);
    }

    @Override
    public Integer save(BiDataCorrEntity entity){
        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public void insertBatch(List<BiDataCorrEntity> entityList) {
        dao.insertBatch(entityList);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return dao.deleteByKey(key , corpid);
    }

    @Override
    public BiDataCorrEntity getByKey(Long key, String corpid) {
        return dao.getByKey(key , corpid);
    }

    @Override
    public List<BiDataCorrEntity> findEntitys(Map<String ,Object> param){
        return dao.findEntitys(param);
    }

    @Override
    public BiDataCorrEntity findByLocation(String corpid,  Long formId, Integer location, String linkAttr, String linkAttrB, String linkAttrC, String linkAttrD, String linkAttrE)  {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("location",location);
        if(StringUtil.isNotEmpty(linkAttrB)){
            param.put("linkAttrSerial",MD5Util.EncoderByMd5(linkAttrB+"_"+linkAttrC+"_"+linkAttrD+"_"+linkAttrE));
        }
        return getBiDataCorrEntity(corpid, formId,linkAttr, param);
    }

    @Override
    public BiDataCorrEntity findByLocation(String corpid,  Long formId, Integer location, String linkAttr, String ... linkAttrSerial)  {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("location",location);
        if(linkAttrSerial!= null && linkAttrSerial.length > 0){
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < linkAttrSerial.length; i++) {
                stringBuilder.append(linkAttrSerial[i].trim());
                if(i < linkAttrSerial.length -1){
                    stringBuilder.append("_");
                }
            }
            param.put("linkAttrSerial",MD5Util.EncoderByMd5(stringBuilder.toString()));
        }
        return getBiDataCorrEntity(corpid, formId,linkAttr, param);
    }

    @Override
    public BiDataCorrEntity findSlaveLocation(String corpid,Long formId, String linkAttr) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("locationNot",1);
        return getBiDataCorrEntity(corpid, formId,linkAttr,param);
    }

    private BiDataCorrEntity getBiDataCorrEntity(String corpid,Long formId, String linkAttr,Map<String, Object> param) {
        param.put("corpid",corpid);
        param.put("formId",formId);
        param.put("linkAttr",linkAttr);
        //TODO 不加del是因为后面要复用
        List<BiDataCorrEntity> list = findEntitys(param);
        // TODO   list的值应该为1  如果不为1,应该做合并操作
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Integer getEntitysCount(Map<String ,Object>  param){
        return dao.getEntitysCount(param);
    }

    @Override
    public Integer updateWarehouseTime(Set<Long> idIn) {
        if(CollectionsUtil.isNotEmpty(idIn)){
            return dao.updateWarehouseTime(idIn);
        }else {
            return 0;
        }
    }

    @Override
    public Integer updateSyscAll(Set<Long> idIn) {
        return  dao.updateSyscAll(idIn);
    }
}
